//
//  CJPopoverController.swift
//  CJPack
//
//  Created by 陈警卫 on 2024-01-11.
//

import UIKit

class CJPopoverController: CJBaseBackController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let btn = UIButton(type: .custom)
        btn.frame = CGRect(x: 10, y: 200, width: 100, height: 30)
        btn.setTitle("popver", for: .normal)
        btn.setTitleColor(.white, for: .normal)
        btn.backgroundColor = .red
        view.addSubview(btn)
        btn.addTarget(self, action: #selector(btnAction(_ :)), for: .touchUpInside)
        
    }
    
    @objc func btnAction(_ sender: UIButton) {
        
        let popVC = UIViewController()
        popVC.view.backgroundColor = .red
        popVC.modalPresentationStyle = .popover
        popVC.preferredContentSize = CGSize(width: 200, height: 200)
        popVC.popoverPresentationController?.sourceView = sender
        popVC.popoverPresentationController?.sourceRect = sender.bounds
        popVC.popoverPresentationController?.permittedArrowDirections = .up
        popVC.popoverPresentationController?.delegate = self
        popVC.popoverPresentationController?.backgroundColor = .blue
        present(popVC, animated: true, completion: nil)
    }
    
}
 
extension CJPopoverController : UIPopoverPresentationControllerDelegate{
    //是否允许点击背景消失
    public func presentationControllerShouldDismiss(_ presentationController: UIPresentationController) -> Bool {
        return true
    }
    //必须设置为none,否则没有弹窗效果
    public func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return .none
    }
    //Dismiss
    func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
        
    }
}
